import React, { Component } from "react"
import { Modal, Form, Switch, InputNumber } from "antd"
import constant from "../loan-setting/constant";
import { messages } from "share/common";
import Chooser from "components/chooser";

@Form.create()
class VendorMaxModal extends Component {

  componentWillReceiveProps(nextProps) {
    if (!nextProps.visible || nextProps.visible === this.props.visible) {
      return -1;
    }
    this.props.form.resetFields();
  }

  onOk = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        const { record, onOk } = this.props;
        value.vendorIds = value.vendorIds.map(item => item.id);
        onOk(Object.assign(record, value));
      }
    })
  };

  render() {
    const { cancel, visible, record, setOfBooksId } = this.props;
    const { getFieldDecorator } = this.props.form;
    const { formItemLayout } = constant;
    return (
      <Modal
        visible={visible}
        onCancel={cancel}
        onOk={this.onOk}
        title={messages('basic-5.key129')/*借款上限*/}
      >
        <Form className='new-form'>
          <Form.Item
            label={messages('common.column.status')/*状态*/}
            {...formItemLayout}
          >
            {getFieldDecorator("enabled", {
              rules: [{ required: true }],
              valuePropName: "checked",
              initialValue: record.hasOwnProperty('enabled') ? record.enabled : true
            })(
              <Switch>{messages('basic-5.key20')/*启用temp*/}</Switch>
            )}
          </Form.Item>
          <Form.Item
            label={messages('basic-5.key130')/*金额上限*/}
            {...formItemLayout}
            extra={messages('basic-5.key131')/*上限金额币种为账套本位币*/}
          >
            {getFieldDecorator('vendorAmountMax',{
              rules: [{ required: true, message: messages('basic-5.key132')/*请输入最大上限总金额*/ }],
              initialValue: record.vendorAmountMax
            })(
              <InputNumber min={0} precision={2} step={1} style={{width:'100%'}} placeholder={messages('common.please.input')/*请输入*/}/>
            )}
          </Form.Item>
          <Form.Item
            label={messages('basic-5.key135')/*适用供应商*/}
            {...formItemLayout}
          >
            {getFieldDecorator('vendorIds',{
              rules: [{ required: true, message: messages('common.select')/*请选择*/ }],
              initialValue: (record.vendorIds || []).map(item => ({ id: item, name: '' }))
            })(
              <Chooser
                type='setOfBooks_vendor'
                listExtraParams={{ setOfBooksId }}
                showDetail={false}
                showNumber
                labelKey='venNickname'
                valueKey='id'
              />
            )}
          </Form.Item>
        </Form>
      </Modal>
    )
  }

}

export default VendorMaxModal
